﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class SysYcjy_kecheng_delete : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            DropDownList1.SelectedIndex = -1;
            DropDownList1.SelectedIndex = -1;
            Session["sTittle"] = "删除课程";    //只能删除未提交的和审核未通过,终审未通过的课程
            DetailsView1.FieldHeaderStyle.Width = 80;
            
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //删除顺序：
        if (DropDownList1.SelectedIndex == -1)
        {
            Response.Write("<script language=javascript>alert('请选择要删除的课程。');if(opener!=null){opener.location.reload();window.close();}</script>");
            return;
        }
        Conn zconn = new Conn();
       //0--首先找到课程对应的论坛序号
        string zsql = "select topic from kecheng where id=" + DropDownList1.SelectedValue.Trim() ;
        string zkecheng_bbsid = zconn.sqlFirst(zsql);
        if (zkecheng_bbsid == "0")
        {
            Response.Write("<script language=javascript>alert('未找到对应论坛，课程未删除,请与系统管理员联系。');if(opener!=null){opener.location.reload();window.close();}</script>");
        }
        //1、删除bbsconfig表中的记录（单记录）--相当于总目录
        zsql = "delete bbsconfig where bbs_id=" +zkecheng_bbsid;
        int zresult1 = zconn.ExeSql(zsql);
        if (zresult1 == 0)
        {
            Response.Write("<script language=javascript>alert('删除对应论坛未成功，课程未删除,请与系统管理员联系。');if(opener!=null){opener.location.reload();window.close();}</script>");
        }
        //2、删除该栏目相关的所有上传文件。涉及到file.exist和file.delete：System.IO.File.Delete( @"c:\abc.txt" );
        zsql = "select title_id from bbs where f_topic='" + zkecheng_bbsid + "/'";
        SqlDataReader zreader1 = zconn.Reader(zsql);
        while (zreader1.Read())
        {
            string ztitleid=zreader1.GetInt64(0).ToString();
            zsql = "select f_filename from ImgUpload where title_id=" + ztitleid;
            string zfilename = zconn.sqlFirst(zsql);
            //zfilename = Server.MapPath("uploadimage") + "/" + zfilename;
            zfilename = Request.PhysicalApplicationPath + "/bbs/uploadimage/" + zfilename;
            
            System.IO.File.Delete(@zfilename);
            
            zsql="delete ImgUpload where where title_id=" + ztitleid;
            zconn.ExeSql(zsql);
        }
        zreader1.Dispose();
        //3、删除bbs表中相关记录（多记录）
        zsql = "delete bbs where f_topic='" + zkecheng_bbsid + "/'";
        zresult1 = zconn.ExeSql(zsql);  //删除 BBS 表中相关数据
        //4、删除kecheng表中记录
        zsql = "delete kecheng where id=" + DropDownList1.SelectedValue.Trim();
        
        int zresult=zconn.ExeSql(zsql);
        //string sql = "insert into bbsconfig (bbsname) values('" + cname + "')";
        
        zconn.Dispose();
        if (zresult == 1)
        {
            DropDownList1.DataBind();
            Response.Write("<script language=javascript>alert('操作正确,课程删除成功。');if(opener!=null){opener.location.reload();window.close();}</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('课程删除过程中出现异常,请记录课程名称并与系统管理员联系。');if(opener!=null){opener.location.reload();window.close();}</script>");
        }
        
    }
    
}
